

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/maple_leaf.png">
  <link rel="icon" href="/blog/img/maple_leaf.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="枫🍁川">
  <meta name="keywords" content="">
  
    <meta name="description" content="第一种扁平数组&amp;#123;     &quot;id&quot;: &quot;01&quot;,     &quot;name&quot;: &quot;张大大&quot;,     &quot;pid&quot;: &quot;-1&quot;,     &quot;job&quot;: &quot;项目经理&quot;   &amp;#125;,   &amp;#123;     &quot;id&amp;quot">
<meta property="og:type" content="article">
<meta property="og:title" content="扁平数组与JSON树结构互转">
<meta property="og:url" content="https://destiny-yyc.github.io/flat-structure-conversion-tree-structure.html">
<meta property="og:site_name" content="枫🍁川的博客">
<meta property="og:description" content="第一种扁平数组&amp;#123;     &quot;id&quot;: &quot;01&quot;,     &quot;name&quot;: &quot;张大大&quot;,     &quot;pid&quot;: &quot;-1&quot;,     &quot;job&quot;: &quot;项目经理&quot;   &amp;#125;,   &amp;#123;     &quot;id&amp;quot">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-24T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-14T07:08:20.289Z">
<meta property="article:author" content="枫🍁川">
<meta property="article:tag" content="扁平数组">
<meta property="article:tag" content="树结构">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>扁平数组与JSON树结构互转 - 枫🍁川的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"destiny-yyc.github.io","root":"/blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>枫🍁川的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="扁平数组与JSON树结构互转"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        枫🍁川
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-01-25 00:00" pubdate>
          2022年1月25日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.6k 字
        
      </span>
    

    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">扁平数组与JSON树结构互转</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="第一种"><a href="#第一种" class="headerlink" title="第一种"></a>第一种</h3><h5 id="扁平数组"><a href="#扁平数组" class="headerlink" title="扁平数组"></a>扁平数组</h5><div class="code-wrapper"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;张大大&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;-1&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;项目经理&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小亮&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品leader&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;03&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小美&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;UIleader&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老马&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;技术leader&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老王&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试leader&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;06&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老李&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;运维leader&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;07&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小丽&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品经理&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;08&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;大光&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品经理&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;09&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小高&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;03&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;UI设计师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;10&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小刘&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;前端工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;11&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小华&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;后端工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;12&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小李&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;后端工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;13&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小赵&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;14&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小强&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;15&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小涛&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;06&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;运维工程师&quot;</span>
  <span class="hljs-punctuation">&#125;</span>
<span class="hljs-punctuation">]</span></code></pre></div>

<h5 id="树形结构"><a href="#树形结构" class="headerlink" title="树形结构"></a>树形结构</h5><div class="code-wrapper"><pre><code class="hljs json"><span class="hljs-punctuation">[</span>
  <span class="hljs-punctuation">&#123;</span>
    <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;张大大&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;项目经理&quot;</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
      <span class="hljs-punctuation">&#123;</span>
        <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小亮&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品leader&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;07&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小丽&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品经理&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;08&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;大光&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;02&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;产品经理&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span>
        <span class="hljs-punctuation">]</span>
      <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
      <span class="hljs-punctuation">&#123;</span>
        <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;03&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小美&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;UIleader&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;09&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小高&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;03&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;UI设计师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span>
        <span class="hljs-punctuation">]</span>
      <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
      <span class="hljs-punctuation">&#123;</span>
        <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老马&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;技术leader&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;10&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小刘&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;前端工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;11&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小华&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;后端工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;12&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小李&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;04&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;后端工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span>
        <span class="hljs-punctuation">]</span>
      <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
      <span class="hljs-punctuation">&#123;</span>
        <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老王&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试leader&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;13&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小赵&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;14&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小强&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;05&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;测试工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span>
        <span class="hljs-punctuation">]</span>
      <span class="hljs-punctuation">&#125;</span><span class="hljs-punctuation">,</span>
      <span class="hljs-punctuation">&#123;</span>
        <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;06&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;老李&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;01&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;运维leader&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
          <span class="hljs-punctuation">&#123;</span>
            <span class="hljs-attr">&quot;id&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;15&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;小涛&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;pid&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;06&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;job&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;运维工程师&quot;</span><span class="hljs-punctuation">,</span>
            <span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span>
          <span class="hljs-punctuation">&#125;</span>
        <span class="hljs-punctuation">]</span>
      <span class="hljs-punctuation">&#125;</span>
    <span class="hljs-punctuation">]</span>
  <span class="hljs-punctuation">&#125;</span>
<span class="hljs-punctuation">]</span></code></pre></div>

<h5 id="转换算法"><a href="#转换算法" class="headerlink" title="转换算法"></a>转换算法</h5><p><strong>「扁平数组」转「树形结构」</strong></p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">treeing</span> (arr) &#123;
  <span class="hljs-keyword">let</span> tree = []
  <span class="hljs-keyword">const</span> map = &#123;&#125;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> arr) &#123;
    <span class="hljs-comment">// 一个新的带children的结构</span>
    <span class="hljs-keyword">let</span> newItem = map[item.<span class="hljs-property">id</span>] = &#123;
      ...item,
      <span class="hljs-attr">children</span>: []
    &#125;
    <span class="hljs-keyword">if</span> (map[item.<span class="hljs-property">pid</span>]) &#123; <span class="hljs-comment">// 父节点已存进map则在父节点的children添加新元素</span>
      <span class="hljs-keyword">let</span> parent = map[item.<span class="hljs-property">pid</span>]
      parent.<span class="hljs-property">children</span>.<span class="hljs-title function_">push</span>(newItem)
    &#125; <span class="hljs-keyword">else</span> &#123; <span class="hljs-comment">// 没有父节点，在根节点添加父节点</span>
      tree.<span class="hljs-title function_">push</span>(newItem)
    &#125;
  &#125;
  <span class="hljs-keyword">return</span> tree
&#125;</code></pre></div>

<p><strong>「树形结构」转「扁平数组」</strong></p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">flatten</span> (tree, arr = []) &#123;
  tree.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> &#123;
    <span class="hljs-keyword">const</span> &#123;children, ...props&#125; = item
    <span class="hljs-comment">// 添加除了children的属性</span>
    arr.<span class="hljs-title function_">push</span>(props)
    <span class="hljs-keyword">if</span> (children) &#123;
      <span class="hljs-comment">// 递归将所有节点加入到结果集中</span>
      <span class="hljs-title function_">flatten</span>(children, arr)
    &#125;
  &#125;)
  <span class="hljs-keyword">return</span> arr
&#125;</code></pre></div>

<h2 id="第二种"><a href="#第二种" class="headerlink" title="第二种"></a>第二种</h2><p><strong>菜单数据结构转换（扁平化数据和树形结构互转）</strong><br>数据格式如下：</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> treeData = [
  &#123;
    <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1&quot;</span>,
    <span class="hljs-attr">children</span>: [
      &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">4</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-1&quot;</span> &#125;,
      &#123;
        <span class="hljs-attr">id</span>: <span class="hljs-number">5</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2&quot;</span>,
        <span class="hljs-attr">children</span>: [
          &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">6</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2-1&quot;</span> &#125;,
          &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">7</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2-2&quot;</span> &#125;,
        ],
      &#125;,
    ],
  &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程2&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程3&quot;</span> &#125;,
];
<span class="hljs-keyword">const</span> flatData = [
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">parent</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">4</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-1&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">5</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">6</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2-1&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">7</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程1-2-2&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程2&quot;</span> &#125;,
  &#123; <span class="hljs-attr">id</span>: <span class="hljs-number">3</span>,<span class="hljs-attr">parent</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&quot;课程3&quot;</span> &#125;,
]</code></pre></div>

<p><strong>1.树形结构转扁平化</strong></p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">TreeToFlat</span>(<span class="hljs-params">data</span>) &#123; 
  <span class="hljs-keyword">let</span> formatData = []
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; data.<span class="hljs-property">length</span>; i++) &#123;
    formatData.<span class="hljs-title function_">push</span>(&#123;
      <span class="hljs-attr">id</span>: data[i].<span class="hljs-property">id</span>,
      <span class="hljs-attr">title</span>: data[i].<span class="hljs-property">title</span>,
    &#125;)
    <span class="hljs-keyword">if</span> (data[i].<span class="hljs-property">children</span>) &#123;
      formatData = formatData.<span class="hljs-title function_">concat</span>(<span class="hljs-title class_">TreeToFlat</span>(data[i].<span class="hljs-property">children</span>));
    &#125;
  &#125;
  <span class="hljs-keyword">return</span> formatData;
&#125;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">TreeToFlat</span>(treeData),<span class="hljs-string">&#x27;输出为扁平化结构&#x27;</span>)</code></pre></div>

<p><strong>2.扁平化数据转为树形结构</strong></p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">FlatToTree</span>(<span class="hljs-params">arr</span>) &#123;
  <span class="hljs-keyword">const</span> map = arr.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">acc, val</span>) =&gt;</span> &#123;
    acc[val.<span class="hljs-property">id</span>] = val
    <span class="hljs-keyword">return</span> acc
  &#125;, &#123;&#125;)
  <span class="hljs-keyword">const</span> tree = []
  arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">region</span> =&gt;</span> &#123;
    <span class="hljs-keyword">if</span> (region.<span class="hljs-property">parent</span>) &#123;
      <span class="hljs-keyword">const</span> parent = map[region.<span class="hljs-property">parent</span>]
      <span class="hljs-keyword">if</span> (!parent.<span class="hljs-property">children</span>) &#123;
        parent.<span class="hljs-property">children</span> = [region]
      &#125;
      <span class="hljs-keyword">else</span> &#123;
        parent.<span class="hljs-property">children</span>.<span class="hljs-title function_">push</span>(region)
      &#125;
    &#125;
    <span class="hljs-keyword">else</span> &#123;
      tree.<span class="hljs-title function_">push</span>(region)
    &#125;
  &#125;)
  <span class="hljs-keyword">return</span> &#123; tree &#125;
&#125;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">FlatToTree</span>(flatData),<span class="hljs-string">&#x27;输出为树形结构&#x27;</span>)</code></pre></div>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/javascript/" class="category-chain-item">javascript</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/%E6%89%81%E5%B9%B3%E6%95%B0%E7%BB%84/">#扁平数组</a>
      
        <a href="/blog/tags/%E6%A0%91%E7%BB%93%E6%9E%84/">#树结构</a>
      
    </div>
  
</div>


              

              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/debounce-throttle.html" title="防抖节流">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">防抖节流</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/hexo-active.html" title="Hexo 文章写作">
                        <span class="hidden-mobile">Hexo 文章写作</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>枫🍁川</span> <i class="iconfont icon-love"></i> <span>前端小白</span> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>
<script type="text/javascript" src="/blog/js/click-love.js"></script>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/blog/live2dw/assets/chitose.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body>
</html>
